const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      "http://g_zb.gitee.io/pages/image/home/cai.png",
      "http://g_zb.gitee.io/pages/image/home/zhao.jpg",
      "http://g_zb.gitee.io/pages/image/home/cai.png"
    ],
    indicatorDots: true,
    interval: 5000,
    duration: 1000,
    bloo: true,
    tuijian: true,
    xinren: true,
    baokuan: false,
    scltop: false,
    user: false,
    meicile: true,
    allhour: 1,
    timer: 1,
    allSec: 2,
    list:[],
    lists:[],
    listss:[],
    listsss:[],
    dates: {
      hours: "03",
      minutes: "00",
      seconds: "00"
    }
  },
  getData:function(){
    wx.showLoading({
      title: "努力加载中"
    });
    var dt=wx.cloud.database();
    var that=this;
    dt.collection('home_shujv').limit(10).where({}).get({
        success:function(res){
          wx.hideLoading();
          that.setData({
            list:res.data
          })
        }
    })
  },

  getDatas: function () {
    wx.showLoading({
      title: "努力加载中"
    });
    var dt = wx.cloud.database();
    var that = this;
    dt.collection('home_shujv').skip(11).limit(7).where({}).get({
      success: function (res) {
        wx.hideLoading();
        that.setData({
          lists: res.data
        })
      }
    })
  },

  getDatass: function () {
    wx.showLoading({
      title: "努力加载中"
    });
    var dt = wx.cloud.database();
    var that = this;
    dt.collection('home_shujv').skip(17).limit(5).where({}).get({
      success: function (res) {
        wx.hideLoading();
        that.setData({
          listss: res.data
        })
      }
    })
  },

  getDatasss: function () {
    wx.showLoading({
      title: "努力加载中"
    });
    var dt = wx.cloud.database();
    var that = this;
    dt.collection('home_shujv').skip(25).limit(3).where({}).get({
      success: function (res) {
        wx.hideLoading();
        that.setData({
          listsss: res.data
        })
      }
    })
  },
// 跳转到授权页面
  shouquan: function() {
    wx.navigateTo({
      url: '../shouquan/shouquan',
    })
  },

// tab切换爆款
  gobaokuan: function() {
    this.setData({
      tuijian: true,
      xinren: true,
      baokuan: false
    })
  },
  // tab切换推荐
  goTuijian: function() {
    this.setData({
      tuijian: false,
      xinren: true,
      baokuan: true
    })
  },
  // tab切换新人
  goXinren: function() {
    this.setData({
      tuijian: true,
      xinren: false,
      baokuan: true
    })
  },
  // 跳转到搜索
  goSearch: function() {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  // 商品详情
  goXiangqing: function(e) {
    var dates = JSON.stringify(this.data.dates);
    var shoplist=e.currentTarget.id;
    console.log(shoplist)
    wx.setStorage({
      key: 'shop',
      data: shoplist,
    })
    if (app.globalData.user) {
      wx.navigateTo({
        url: '../spxq/spxq?data=' + dates,
      })
    } else {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getData();
    this.getDatas();
    this.getDatass();
    this.getDatasss();
  },

// 灵动菜单
  onPageScroll: function(e) {
    var that = this;
    if (e.scrollTop > 480) {
      that.setData({
        scltop: true
      })
    } else {
      that.setData({
        scltop: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // 倒计时
    var hour = this.data.allhour * 1;
    var allsecond = this.data.allsecond;

    switch (hour) {
      case 1:
        allsecond = this.data.dates.hours * 1 * 3600;
        break;
      case 2:
        allsecond = this.data.dates.minutes * 1 * 60;
        break;
      case 3:
        allsecond = this.data.dates.seconds * 1;
        break;
      default:
        break;
    }


    var that = this;
    that.data.timer = setInterval(function() {
      that.setData({
        dates: {
          hours: addZero(Math.floor(allsecond / 3600)),
          minutes: addZero(Math.floor((allsecond % 3600) / 60)),
          seconds: addZero(allsecond % 60)
        }
      })
      allsecond--
      if (allsecond < 0) {
        clearInterval(that.data.timer);
        that.data.timer = null;
      }

      function addZero(n) {
        n < 10 ? n = "0" + n : n
        return n;
      }
    }, 1000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      user: app.globalData.user,
      meicile: app.globalData.meicile
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.clearStorageSync();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    wx.showNavigationBarLoading({
      success: function () {
        that.getData();
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.list.length == this.data.nub) {
      wx.showLoading({
        title: "没有更多数据啦"
      });
    } else {
      wx.showLoading({
        title: "努力加载中"
      });
    }
    var that = this;
    var dbs = wx.cloud.database();
    dbs.collection('home_shujv').where({
    }).skip(that.data.list.length).get({
      success: function (res) {
        that.setData({
          list: that.data.list.concat(res.data)
        })
        wx.hideLoading();
      }
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

})